import Vue from 'vue'
import Vuex from 'vuex'
import store from '../store'
import utils from './utils'
let vue = Vue.use(Vuex)
function isNotEmpty(row, column) {
  return row.hasOwnProperty(column.property)
    && row[column.property] != null
    && row[column.property] != undefined
}

function isPropNotEmpty(row, prop) {
  return row.hasOwnProperty(prop)
    && row[prop] != null
    && row[prop] != undefined
}

export default class approveUtils {
  constructor(options) {
  }

  getCloumns(){
    return [
      { prop: 'creatorName', label: '申请人', sortable: true, width: '130', formatter: this.stringFormater },
      { prop: 'createTime', label: '申请时间', sortable: true, width: '220', formatter: this.timeStampFormater },
      { prop: 'type', label: '变更类型', sortable: true, width: '150', formatter:(row,column) => this.getAppvoalType(row,column) },
      { prop: 'state', label: '申请状态', sortable: true, width: '150', formatter:(row,column) => this.getAppvoalStatus(row,column) },
      { prop: 'reason', label: '原因', sortable: true, width: '350', formatter: this.stringFormater }
    ]
  }

  getSubmitCols(){
    return [
      { prop: 'approverName', label: '待办人', sortable: true, width: '180', formatter: this.stringFormater },
      { prop: 'createTime', label: '申请时间', sortable: true, width: '180', formatter: this.timeStampFormater },
      { prop: 'type', label: '变更类型', sortable: true, width: '150', formatter:(row,column) => this.getAppvoalType(row,column) },
      { prop: 'reason', label: '原因', sortable: true, width: '350', formatter: this.stringFormater },
      { prop: 'state', label: '申请状态', sortable: true, width: '150', formatter:(row,column) => this.getAppvoalStatus(row,column) },
      { prop: 'ackTime', label: '审核时间', sortable: true, width: '180', formatter:this.timeStampFormater },
      { prop: 'comment', label: '审核意见', sortable: true, width: '150', formatter:this.stringFormater }
    ]
  }

  getDiffCols(){
    return [
      { prop: 'field', label: '属性', sortable: true, width: '180', formatter: this.fieldFormater },
      { prop: 'before', label: '变更前', sortable: true, width: '180', formatter: this.beforeFormater },
      { prop: 'after', label: '变更后', sortable: true, width: '180', formatter: this.afterFormater }
    ]
  }

  getProcs(){
    return [
      {id:1,name:'同意'},
      {id:2,name:'拒绝'}
    ]
  }

  getTypes(){
    return [
        {id:0,name:'新增用户'},
        {id:1,name:'编辑用户'},
        {id:2,name:'删除用户'}
      ]
  }

  getStatus(){
      return [
          {id:0,name:'待处理'},
          {id:1,name:'同意'},
          {id:2,name:'拒绝'}
      ]
  }

  getApproval(approvalId,sucCallback,failedCallback){
    vue.http.post('/hr/approval/get?id='+approvalId).then(response => {
      console.log("get approval response" + response);
        if(sucCallback){
          if (response.body.result == 1) {
            console.log('get approval response ' + JSON.stringify(response.body));
            sucCallback(response.body.body)
          }
          else{
            failedCallback(response)
          }
        }
    }, response => {
      if(failedCallback){
        failedCallback(response)
      }
    })
  }

  agree(approval,sucCallback,failedCallback){
    vue.http.post('/hr/approval/agree?id='+approval.id+"&comment="+approval.comment).then(response => {
      console.log("agree approver response" + response);
        if(sucCallback){
          sucCallback(response)
        }
    }, response => {
      if(failedCallback){
        failedCallback(response)
      }
    })
  }

  reject(approval,sucCallback,failedCallback){
    vue.http.post('/hr/approval/reject?id='+approval.id+"&comment="+approval.comment).then(response => {
      console.log("reject approver response" + response);
        if(sucCallback){
          sucCallback(response)
        }
    }, response => {
      if(failedCallback){
        failedCallback(response)
      }
    })
  }
  
  stringFormatter(obj, prop) {
    return obj
  }

  categoryFormatter(categories,obj,prop){
    for (var index in categories) {
      if (categories[index].id == obj) {
        return categories[index].name
      }
    }
    return ''
  }

  departFormater(departments,obj,prop){
    if(!departments){
      return ''
    }
    console.log('departFormater:'+departments);
    for(var index in departments){
      if(departments[index].id == obj){
        return departments[index].name
      }
      else{
        var res = this.departFormater(departments[index].subDepartments,obj,prop)
        if(res){
          return res
        }
      }
    }
    return ''
  }

  positionlevelFormatter(postTypes,sysconfigs, positionLevel, prop){
    for(var index in postTypes){
      var positionLevels =sysconfigs['positionlevels_'+postTypes[index].id]
      for(var plIndex in positionLevels){
        if(positionLevel == positionLevels[plIndex].id){
          return positionLevels[plIndex].name
        }
      }
    }
    return ''
  }

  positionFormater(positions, obj, prop) {
      var position = ''
      var poss = []
      if(typeof(obj) == 'string'){
        poss = obj.split(',')
      }
      else if(obj instanceof Number){
        poss.push(obj)
      }
      if (poss) {
        for (var index in positions) {
          for (var posIndex in poss) {
            if (poss[posIndex] == positions[index].id) {
              if (position) {
                position = position + ',' + positions[index].name
              }
              else {
                position = positions[index].name
              }
            }
          }
        }
      }
      return position
  }

  nativePlaceFormatter(provinces, cities, nativePlace) {
      var strArray = nativePlace.split(',')
      var strNativePlace = null
      for (var index in provinces) {
        if (provinces[index].code == strArray[0]) {
          strNativePlace = provinces[index].name
          break
        }
      }

      for (var index in cities) {
        if (cities[index].code == strArray[1]) {
          if (!strNativePlace) {
            strNativePlace = cities[index].name
          }
          else {
            strNativePlace = strNativePlace + cities[index].name
          }
          break
        }
      }
    
    return strNativePlace
  }

  fieldFormater(row,column){
    if(isNotEmpty(row,column)){
      return differCols[row[column.property]].title
    }
  }

  beforeFormater(row,column){
    if(isNotEmpty(row,column)){
      var formatter = differCols[row.field].formatter
      return formatter(row.before)
    }
    return ''
  }

  afterFormater(row,column){
    if(isNotEmpty(row,column)){
      var formatter = differCols[row.field].formatter
      return formatter(row.after)
    }
    return ''
  }

  getAppvoalType(row, column){
    var status = row[column.property]
    for(var index in this.getTypes()){
      if(this.getTypes()[index].id == status){
        return this.getTypes()[index].name
      }
    }
    return ''
  }

  ack(row,sucCallback,failedCallback){
    vue.http.post('/hr/approval/get?id='+row.id).then(response => {
      console.log("get approver response" + response);
        if(sucCallback){
          sucCallback(response)
        }
    }, response => {
      if(failedCallback){
        failedCallback(response)
      }
    })
  }

  getAppvoalStatus(row, column){
    var status = row[column.property]
    for(var index in this.getStatus()){
      if(this.getStatus()[index].id == status){
        return this.getStatus()[index].name
      }
    }
    return ''
  }

  timeStampFormater(row, column) {
    if (isNotEmpty(row, column)) {
      return approveUtils.timeStamp2String(row[column.property])
    }
    return ''
  }

  getUserApprovals(sucCallback,failedCallback){
    vue.http.get('/hr/approval/listMyHandles').then(response => {
      console.log("list approver response" + response);
        if(sucCallback){
          sucCallback(response)
        }
    }, response => {
      if(failedCallback){
        failedCallback(response)
      }
    })
  }

  getUserSubmits(sucCallback,failedCallback){
    vue.http.get('/hr/approval/listMySubmit').then(response => {
      console.log("list approver response" + response);
        if(sucCallback){
          sucCallback(response)
        }
    }, response => {
      if(failedCallback){
        failedCallback(response)
      }
    })
  }

  static timeStamp2String(time) {
    var datetime = new Date();
    datetime.setTime(time * 1000);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1;
    var date = datetime.getDate();
    var hour = datetime.getHours();
    var minute = datetime.getMinutes();
    return year + "/" + month + "/" + date + " " + hour +":" + minute;
  }

  listApprover(sucCallback,failedCallback){
    if(store.state.approvers && store.state.approvers.length >0){
      return
    }
    vue.http.post('/hr/approval/listApprover').then(response => {
      console.log("list approver response" + response);
      if (response.body.result == 1) {
        store.commit('SET_APPROVERS', response.body.body)
        if(sucCallback){
          sucCallback(response)
        }
      }
    }, response => {
      if(failedCallback){
        failedCallback(response)
      }
    })
  }
}
